package com.xiaobaili.consumer.controller;

import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/classicSend")
    public void classicSend() {
        // 队列名称
        String queueName = "simple.queue";
        // 消息
        String message = String.format("%s-%s-hello, spring amqp,", Thread.currentThread().getName(), LocalDateTime.now());
        // 发送消息
        rabbitTemplate.convertAndSend(queueName, message);
    }


    @SneakyThrows
    @GetMapping("/wordQueue")
    public void wordQueue() {
        // 队列名称
        String queueName = "work.queue";
        // 消息
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = now.format(dtf);
        String message = String.format("%s-%s", Thread.currentThread().getName(), format);
        // 发送消息
        for (int i = 0; i < 100; i++) {
            String sendMessage = message + "【" + i + "】";
            rabbitTemplate.convertAndSend(queueName, sendMessage);
            Thread.sleep(20);
        }
    }

    @SneakyThrows
    @GetMapping("/fanoutExchange")
    public void fanoutExchange() {
        //交换机名称
        String exchangeName = "xiaobaili.fanout";
        //发送的内容
        String message = "hello everyone!";
        //发送消息
        rabbitTemplate.convertAndSend(exchangeName, "", message);
    }

    @SneakyThrows
    @GetMapping("/directExchange")
    public void directExchange() {
        //交换机名称
        String exchangeName = "xiaobaili.direct";
        //发送的内容
        String message = "hello everyone!";
        //发送消息
        rabbitTemplate.convertAndSend(exchangeName, "", message);

        for (int i = 0; i < 10; i++) {
            String sendMessage = message + "【" + i + "】";
            if (i % 2 == 0) {
                rabbitTemplate.convertAndSend(exchangeName, "queue0", sendMessage);
                if (i % 4 == 0) {
                    rabbitTemplate.convertAndSend(exchangeName, "queue1", sendMessage);
                }
            } else {
                if (i % 3 == 0) {
                    rabbitTemplate.convertAndSend(exchangeName, "queue3", sendMessage);
                } else {
                    rabbitTemplate.convertAndSend(exchangeName, "queue2", sendMessage);
                }
            }
        }
    }

    @SneakyThrows
    @GetMapping("/topicExchange")
    public void topicExchange() {
        //交换机名称
        String exchangeName = "xiaobaili.topic";
        for (int i = 0; i < 10; i++) {
            switch (i) {
                case 0:
                    rabbitTemplate.convertAndSend(exchangeName, "china.weather", "中国天气");
                    break;
                case 1:
                    rabbitTemplate.convertAndSend(exchangeName, "china.qq", "中国QQ");
                    break;
                case 2:
                    rabbitTemplate.convertAndSend(exchangeName, "china.weixin", "中国微信");
                    break;
                case 3:
                    rabbitTemplate.convertAndSend(exchangeName, "china.news", "中国新闻");
                    break;
                case 4:
                    rabbitTemplate.convertAndSend(exchangeName, "japan.news", "日本新闻");
                    break;
                case 5:
                    rabbitTemplate.convertAndSend(exchangeName, "American.news", "美国新闻");
                    break;
                case 6:
                    rabbitTemplate.convertAndSend(exchangeName, "china.stock", "中国股票");
                    break;
                default:
                    break;
            }
        }
    }
}
